package bcontractor.partialmeet;

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import bcontractor.api.Sentence;
import bcontractor.api.SentenceSet;

/**
 * Represents a RemainderSet. A remainder set contains all maximal subsets of
 * sentences of a knowledge base that fail to entail a sentence.
 * 
 * @author lundberg
 * 
 */
public class RemainderSet<S extends Sentence<S>> implements Iterable<SentenceSet<S>> {

    private final Set<SentenceSet<S>> elements;

    /**
     * Constructor
     * 
     * @param elements
     *            elements
     */
    public RemainderSet(Set<SentenceSet<S>> elements) {
        this.elements = elements;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<SentenceSet<S>> iterator() {
        return this.elements.iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof RemainderSet<?>)) {
            return false;
        }
        RemainderSet<?> o = (RemainderSet<?>) obj;
        return new EqualsBuilder().append(this.elements, o.elements).isEquals();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.elements).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("RemainderSet:");
        builder.append("\n\tElements:");
        for (SentenceSet<S> e : this.elements) {
            builder.append("\n\t\t");
            builder.append(e.toString().trim().replaceAll("\n", "\n\t\t"));
        }
        return builder.toString();
    }
}
